package org.boticelli.plugin.twitter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ShortURLResolverImplTestCase
{
    private static Logger log = LoggerFactory.getLogger(ShortURLResolverImplTestCase.class);
    
    private ShortURLResolverImpl resolver;

    @Before
    public void init() throws KeyManagementException, NoSuchAlgorithmException
    {
        resolver = new ShortURLResolverImpl();
        resolver.setDomainRegex("(http:\\/\\/(bit\\.ly|trunc\\.it|is\\.gd|tinyurl\\.com|doiop\\.com|snipurl\\.com|cl\\.gs|twurl\\.nl)/[^\\s*]+)");
        resolver.setHttpClientFactory(new HttpClientFactory());
        
    }
    
    @Test
    public void test()
    {
        
        assertShortUrlWorks("xxx http://bit.ly/22teEQ eee");
        assertShortUrlWorks("yyy http://trunc.it/2bwi8 ddd");
        assertShortUrlWorks("zzz http://is.gd/1 ccc");
        assertShortUrlWorks("aaa http://tinyurl.com/unicycles bbb");
        assertShortUrlWorks("aaa http://doiop.com/t1b2p0 bbb");
        assertShortUrlWorks("aaa http://snipurl.com/sab7x bbb");
        assertShortUrlWorks("<fforw> ignore this http://cl.gs/EyUhQQ xxx");
        assertShortUrlWorks("aaa http://twurl.nl/kbdqg4 bbb");
    }

    private void assertShortUrlWorks(String in)
    {
        String out = resolver.resolveShortURL(in);
        
        log.debug("in: {}, out: {}", in, out);
        
        assertThat(in, is(not(out)));
    }

}
